﻿/*///////////////////////////////////////////////////////////
Checkbox.as
Ben Smith
2009
///////////////////////////////////////////////////////////*/

package com.bsmith.common.ui 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	/*===========================================================
	Checkbox
	Represents a ui control checkbox
	===========================================================*/
	public class Checkbox extends MovieClip
	{
		static public const EVENT_TOGGLED_ON:String = "cb.toggled.on";
		static public const EVENT_TOGGLED_OFF:String = "cb.toggled.off"
		
		public var _stxCheckmark:TextField;
		
		private var _bChecked:Boolean = false;	// Is this checked?
		
		/*-----------------------------------------------------------
		Checkbox - ctor
		-----------------------------------------------------------*/
		public function Checkbox() 
		{
			initialize();
		}
		
		/*-----------------------------------------------------------
		onRemoved
		-----------------------------------------------------------*/
		private function onRemoved(e:Event):void 
		{
			this.removeEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
			this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOverEvent);
			this.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOutEvent);
			this.removeEventListener(MouseEvent.CLICK, onMouseClick);
			this.addEventListener(Event.ADDED_TO_STAGE, initialize, false, 0, true);
		}
		
		/*-----------------------------------------------------------
		initialize
		Initializes checkbox for use
		-----------------------------------------------------------*/
		private function initialize(e:Event=null):void 
		{
			if (e)
				this.removeEventListener(Event.ADDED_TO_STAGE, initialize);
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverEvent, false, 0, true);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutEvent, false, 0, true);
			this.addEventListener(MouseEvent.CLICK, onMouseClick, false, 0, true);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemoved, false, 0, true);
			
			setChecked(false);
		}
		
		/*-----------------------------------------------------------
		onMouseClick
		Toggles state
		-----------------------------------------------------------*/
		private function onMouseClick(e:MouseEvent):void 
		{
			toggle();
		}
		
		/*-----------------------------------------------------------
		onMouseOutEvent
		Sets state when moused out
		-----------------------------------------------------------*/
		private function onMouseOutEvent(e:MouseEvent):void 
		{
			this.gotoAndStop(1);
		}
		
		/*-----------------------------------------------------------
		onMouseOverEvent
		Sets state when moused over
		-----------------------------------------------------------*/
		private function onMouseOverEvent(e:MouseEvent):void 
		{
			this.gotoAndStop(2);
		}
		
		/*-----------------------------------------------------------
		toggle
		Toggles checkbox state
		-----------------------------------------------------------*/
		private function toggle():void
		{
			setChecked(!_bChecked);
		}
		
		/*###########################################################
		GETTERS AND SETTERS
		###########################################################*/
		public function isChecked():Boolean { return _bChecked; }
		public function setChecked(value:Boolean, fireEvent:Boolean=true):void 
		{
			_bChecked = value;
			_stxCheckmark.visible = value;
			if (fireEvent)
				this.dispatchEvent(new Event(value ? EVENT_TOGGLED_ON : EVENT_TOGGLED_OFF));
		}
	}
}